#ifndef POPUP_FILE_IO_WINDOWS_H
#define POPUP_FILE_IO_WINDOWS_H

struct PopupFile
{
  typedef enum { 
    READ   = 0x01,
    WRITE  = 0x02,
    CREATE = 0x04
  } AccessMode;

  CFile m_file;

  PopupFile(const std::string & p_filePath, const char *p_mode)
	  : m_file(CString(p_filePath.c_str()), windowsAccessMode(p_mode)) {}
  
  ~PopupFile() { m_file.Close(); }

  size_t read(void *p_buffer, size_t p_nbBytes)
  {
    try {
      return m_file.Read(p_buffer, p_nbBytes);
    }
    catch(CFileException e)
    {
      return 0;
    }
  }
  
  size_t write(void *p_buffer, size_t p_nbBytes)
  {
    try {
      m_file.Write(p_buffer, p_nbBytes);
      return p_nbBytes;
    }
    catch(CFileException e)
    {
      return 0;
    }	
  }
  

  UINT windowsAccessMode(const char *p_mode)
  {
    UINT _mode = 0;
    
    if (strstr(p_mode, "b") != 0)
    {
      _mode = (_mode|CFile::typeBinary);
    }
    if (strstr(p_mode, "r") != 0)
    {
      _mode = (_mode|CFile::modeRead);
    }
    if (strstr(p_mode, "w") != 0)
    {
      _mode = (_mode|CFile::modeWrite|CFile::modeCreate);
    }
    
    return _mode;
  }
};

#endif
